﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Core;
using Entity;

namespace CSPTest.Admin
{
    public partial class Customer : EntityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected override EntityDataSource EntitySource
        {
            get
            {
                return this.edsMain;
            }
        }

        protected override TextBox EntitySearchTxt
        {
            get
            {
                return this.SearchText;
            }
        }

        protected override Button EntityAddBtn
        {
            get
            {
                return this.BtnAdd;
            }
        }

        protected override Button EntityClearBtn
        {
            get
            {
                return this.BtnClear;
            }
        }

        protected override GridView EntityGrid
        {
            get
            {
                return gvMain;
            }
        }

        protected override DetailsView EntityDetails
        {
            get
            {
                return dvMain;
            }
        }

        //TODO: remove logic code to BL
        protected override void OnEntityDisabling(int entityKey)
        {
            using (var dc = new CSPEntities())
            {
                dc.Customers.First(p => p.CustomerID == entityKey).Disabled = true;
                dc.SaveChanges();
            }
        }

        protected override string GetEntitySortExpression(string keyword)
        {
            return "(it.Name like N'%" + EntitySearchTxt.Text + "%')";
        }

        //TODO: remove logic code to BL
        protected override void OnEntityInserting(EntityDataSourceChangingEventArgs e)
        {
            var customer = e.Entity as Entity.Customer;

            var relativeNameList = this.Request.Form["relative-name"];
            var relativeRelationshipList = this.Request.Form["relative-relationship"];
            var relativeJobList = this.Request.Form["relative-job"];
            var relativeMobileList = this.Request.Form["relative-mobile"];

            if (relativeNameList != null)
            {
                var rname = relativeNameList.Split(',');
                var rship = relativeRelationshipList.Split(',');
                var rjob = relativeJobList.Split(',');
                var rmob = relativeMobileList.Split(',');

                for (int i = 0; i < rname.Length; i++)
                {
                    if (!String.IsNullOrEmpty(rname[i]))
                    {
                        customer.Relatives.Add(new Relative()
                        {
                            Name = rname[i],
                            Relationship = rship[i],
                            Job = rjob[i],
                            Mobile = rmob[i],
                        });
                    }
                }
            }
        }

        //TODO: remove logic code to BL
        protected override void OnEntityUpdating(EntityDataSourceChangingEventArgs e)
        {
            var customer = e.Entity as Entity.Customer;

            customer.Relatives.Load();
            foreach (var r in customer.Relatives)
            {
                r.Disabled = true;
            }

            OnEntityInserting(e);
        }

        protected IEnumerable GetRelatives(object source)
        {
            return from t in source as IEnumerable<Entity.Relative>
                   where !t.Disabled
                   select t;
        }
    }
}